import java.awt.*; import java.awt.event.*; class MyFrame extends Frame implements ActionListener { Button btn; MyFrame() { setLayout(new FlowLayout()); btn=new Button("OK"); btn.addActionListener(this); add(btn); } public void actionPerformed(ActionEvent e) { setTitle("Welcome to AWT Events"); } } class demo { public static void main(String args[]) { MyFrame frm=new MyFrame(); frm.setVisible(true); frm.setSize(400,400); } }
import java.awt.*; import java.awt.event.*; class MyFrame extends Frame implements ActionListener { Button b1,b2; public MyFrame() { setLayout(new FlowLayout()); b1=new Button("RED"); b2=new Button("BLUE"); b1.addActionListener(this); b2.addActionListener(this); add(b1); add(b2); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) setBackground(Color.RED); else setBackground(Color.BLUE); } } class demo { public static void main(String args[]) { MyFrame frm=new MyFrame(); frm.setSize(400,300); frm.setVisible(true); } }